package com.academicsystem.www.dao;

import com.academicsystem.www.model.Lesson;
import com.academicsystem.www.model.Major;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author Jacky
 */
@Mapper
public interface Dao {
    /**
     * 获取专业名称
     * @return 专业名称列表
     */
    @Select("select * from major")
    List<Major> getMajorTable();
    /**
     * 根据课程id获取课程信息
     * @param id 课程id
     * @return 课程信息
     */
    @Select("select * from lesson where id=#{id}")
    Lesson getLessonById(Integer id);

    /**
     * 根据专业id查询专业所学的所有课程
     * @param majorId 查询专业id
     * @return 查询专业所需学的课程列表
     */
    @Select("select * from lesson where major=#{majorId} order by term")
    List<Lesson> getLessonByMajor(Integer majorId);

    /**
     * 获取专业课程数量
     * @param majorId 专业id
     * @return 该专业课程数量
     */
    @Select("select count(*) from lesson where major=#{majorId}")
    Integer getLessonCount(Integer majorId);

    /**
     * 根据课程id获取该课程的所有先修课程id
     * @param lessonId 课程id
     * @return 先修课程的id列表
     */
    @Select("select before_lesson_id from rel_lesson_before where lesson_id=#{lessonId}")
    List<Integer> getBeforeLessonId(Integer lessonId);

    /**
     * 为课程添加学期
     * @param lessonId 课程id
     * @param term 学期
     */
    @Update("update lesson set term=#{term} where id=#{lessonId}")
    void addTerm(Integer lessonId,Integer term);

    /**
     * 添加专业
     * @param majorName 专业名称
     */
    @Insert("insert into major(id, major_name) values(null,#{majorName}) ")
    void addMajor(String majorName);

    /**
     * 获取专业id
     * @param majorName 专业名
     * @return 专业id
     */
    @Select("select id from major where major_name=#{majorName}")
    Integer getMajorId(String majorName);

    /**
     * 删除专业
     * @param id 专业id
     */
    @Delete("delete from major where id=#{id}")
    void deleteMajor(Integer id);

    /**
     * 根据专业id删除课程
     * @param major 专业id
     */
    @Delete("delete from lesson where major=#{major}")
    void deleteLessonByMajor(Integer major);

    /**
     * 删除先修关系
     * @param lessonId 课程id
     */
    @Delete("delete from rel_lesson_before where lesson_id=#{lessonId}")
    void deleteRelByLesson(Integer lessonId);

    /**
     * 添加课程
     * @param lesson 课程实体
     */
    @Insert("insert into lesson(id,name,credit,term,major) values (null,#{name},#{credit},null,#{major})")
    void addLesson(Lesson lesson);

    /**
     * 获取除某课程以外的其他课程
     * @param lessonId 课程id
     * @param majorId 专业id
     * @return 课程列表
     */
    @Select("select * from lesson where id!=#{lessonId}&&major=#{majorId}")
    List<Lesson> getLessonExcept(Integer lessonId,Integer majorId);


    @Insert("insert into rel_lesson_before(id, lesson_id, before_lesson_id) values (null,#{lessonId},#{beforeLessonId})")
    void setBeforeLesson(Integer lessonId,Integer beforeLessonId);
}
